//
//  MLZPostingViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/8.
//  Copyright © 2016年 zj. All rights reserved.
//

#import "MLZPostingViewController.h"

@interface MLZPostingViewController ()
@property (nonatomic, strong)UITextView*textview;
@property (nonatomic, strong)UITextField *titlefild;
@property (nonatomic, strong)UIButton *Posting;
@end

@implementation MLZPostingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addtextview];

}
-(void)addtextview{
    _titlefild = [[UITextField alloc]initWithFrame:CGRectMake(CLandR, CTandB*3+64, KW-CLandR*2, 30)];
    _titlefild.borderStyle = UITextBorderStyleRoundedRect;
    _titlefild.textAlignment= NSTextAlignmentCenter;
    _titlefild.placeholder =@"请输入标题";
    [self.view addSubview:_titlefild];
    

   _textview = [[UITextView alloc]initWithFrame:CGRectMake(CLandR, _titlefild.bottom+CTandB, KW-CLandR*2, 300)];
  
    _textview.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:_textview];

    _Posting = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    _Posting.frame = CGRectMake(CLandR+((KW-CLandR*2)/2)/2, _textview.bottom+CTandB, (KW-CLandR*2)/2 , 100);
    _Posting.backgroundColor = [UIColor greenColor];
    [_Posting.layer setCornerRadius:_Posting.width/10];
    _Posting.layer.masksToBounds= YES;
    [_Posting addTarget:self action:@selector(postingAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_Posting setTitle:@"发帖" forState:(UIControlStateNormal) ];
    [self.view addSubview:_Posting];
    
    //返回键
    UIButton *changeFrame = [UIButton buttonWithType:(UIButtonTypeCustom)];
    changeFrame.frame = CGRectMake(10, 30, 30, 30);
    [changeFrame setImage:[UIImage imageNamed:@"jiantou"] forState:(UIControlStateNormal)];
    //changeFrame.backgroundColor = [UIColor blackColor];
    [changeFrame addTarget:self action:@selector(changeFramAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:changeFrame];





}
-(void)postingAction:(UIButton*)button{
   
    NSUserDefaults *userqq = [NSUserDefaults standardUserDefaults];
    NSString*name = [userqq objectForKey:@"username"];
    NSString*imageIcon = [userqq objectForKey:@"imageIcon"];
    if ([[BmobUser getCurrentUser].username isEqualToString:name]) {
        
    
    BmobUser *user = [BmobUser getCurrentUser];
    
    BmobObject*object  = [BmobObject objectWithClassName:@"ACM_"];
    [object setObject:_textview.text forKey:@"contents"];
    [object setObject:user.username forKey:@"userName"];
    [object setObject:_titlefild.text forKey:@"title"];
    [object setObject:imageIcon forKey:@"picture"];//吧qq头像赋值给这个
    [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        
        if(isSuccessful){
          
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发帖成功" delegate: self cancelButtonTitle:nil otherButtonTitles:nil];
            [alertView show];
            
            // 1秒消失
            [self performSelector:@selector(alertViewDismiss1:) withObject:alertView afterDelay:1];
           [self dismissViewControllerAnimated:YES completion:nil];
            if([_delegate respondsToSelector:@selector(reloadpost)]){
                
                [_delegate reloadpost];
                
            }
            

        }
        
        // 1秒消失
        
        
        
        
    }];
   

    }else{
    
        if ([BmobUser getCurrentUser]) {
            
            
            BmobUser *user = [BmobUser getCurrentUser];
            
            BmobObject*object  = [BmobObject objectWithClassName:@"ACM_"];
            [object setObject:_textview.text forKey:@"contents"];
            [object setObject:user.username forKey:@"userName"];
            [object setObject:_titlefild.text forKey:@"title"];
            [object setObject:@"" forKey:@"picture"];//吧qq头像赋值给这个
            [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                
                if(isSuccessful){
                    
                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发帖成功" delegate: self cancelButtonTitle:nil otherButtonTitles:nil];
                    [alertView show];
                    
                    // 1秒消失
                    [self performSelector:@selector(alertViewDismiss1:) withObject:alertView afterDelay:1];
                    [self dismissViewControllerAnimated:YES completion:nil];
                    if([_delegate respondsToSelector:@selector(reloadpost)]){
                        
                        [_delegate reloadpost];
                        
                    }
                    
                    
                }
                
                // 1秒消失
                
                
                
                
            }];
            

    
    
    
    }


}
}
- (void)alertViewDismiss1:(UIAlertView *)alertView
{
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}




-(void)changeFramAction:(UIButton*)button
{
    
    
    
//    if([_delegate respondsToSelector:@selector(reloadpost)]){
//        
//        [_delegate reloadpost];
//        
//    }
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
    
}
- (UIAlertController *)alertActionWithTitle:(NSString *)title message:(NSString *)message other:(NSString *)other
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:nil];
    [alertVc addAction:otherAction];
    [self presentViewController:alertVc animated:YES completion:nil];
    return alertVc;
}
-( void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [_titlefild resignFirstResponder];
    [_textview resignFirstResponder];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
